import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import Set from '../views/Set.vue'

const routes: RouteRecordRaw[] = [
    {
        path: '/login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/reg',
        component: () => import('../views/Reg.vue')
    },
    {
        path: '/',
        redirect: '/login',
        component: () => import('../components/Layout.vue'),
        children: [

            {
                path: 'index',
                component: () => import('../views/Index.vue')
            },
            {
                path: 'alarm',
                component: () => import('../views/Alarm.vue')
            },
            {
                path: 'set',
                component: Set
            },
            {
                path: 'iot',
                component: () => import('../views/IOT.vue')
            },
            {
                path: 'add',
                component: () => import('../views/ADD.vue')
            },
        ]
    },
    {
        path:'',
        redirect:'/index'
    }
]
const router = createRouter({
    routes,
    history: createWebHistory(),
})
router.beforeEach((to, form, next) => {
    if (['/login', '/reg'].includes(to.path)) {
        next()
        return
    }
    if (!localStorage.getItem('token')) {
        next('/login')
        return
    }
    next()
})
export default router